package fun.wlfj.j2t.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Clazz extends Element {
	// 类里面有什么？有多个构造器、属性、方法
	public List<Constructer> consList = new ArrayList<>();
	public List<Property> properList = new ArrayList<>();
	public List<Method> methodList = new ArrayList<>();
	
	public void addCons(Constructer c) {
		consList.add(c);
	}
	
	public void addProper(Property p) {
		properList.add(p);
	}
	
	public Property getProperByName(String name) {
		return (Property)(properList.stream().filter(p -> p.name.equals(name)).toArray()[0]);
	}
	
	public void addMeth(Method m) {
		methodList.add(m);
	}
	
	public Clazz(String name) {
		super(name);
	}
	
	@Override
	public String getRepr() {
		StringBuilder sb = new StringBuilder();
		sb.append("Class: " + name + "\n");
		
		String head = "|--> ";
		for(Property p : properList) {
			// 这里需要缩进
			int indent = head.length() + p.name.length() + 2 - 1;
			// 现在我们要按照行处理内容
			Scanner lineByLine = new Scanner(p.getRepr());
			boolean isFirstLine = true;
			while(lineByLine.hasNext()) {
				String line = lineByLine.nextLine();
				if(isFirstLine) {
					isFirstLine = false;
					line = head + line;
					sb.append(line + "\n");
				}else {
					line = "|" + line.indent(indent);
					sb.append(line);
				}
				
			}
		}
		for(Constructer con : consList) {
			sb.append(head + con.strRepr() + "\n");
		}
		for(Method me : methodList) {
			sb.append(head + me.strRepr() + "\n");
		}
		sb.append("-\n");
		
		return sb.toString();
	}
}
